import React, {Component} from 'react';
import NavigationBar from '../../Common/NavigationBar/NavigationBar.jsx'
import RegisterItem from '../../Common/RegisterLoginItem/RegisterLoginItem.jsx';
//import Using ES6 syntax
import WeUI from 'react-weui';

//import styles
import 'weui';
import 'react-weui/build/dist/react-weui.css';
import {CSS} from '../styles/Register.css'
const {Toptips} = WeUI;
export default class ForgetPassword extends Component {
    constructor(props) {
        super(props);
        this.LeftClick = this.LeftClick.bind(this);
        this.forgetPassword = this.forgetPassword.bind(this);

    }

    componentWillMount() { //组件即将被插入
        this.state = {
            showWarn: false,
            warnTimer: null,
            tipText: "",
            data: {
                title: "忘记密码",
                describe: "验证信息",
                inputName1: "手机号",
                inputName2: "验证码",
                messagesCodeTitle: "获取验证码",
                forgetPassword: "",
                buttonTitle: "下一步",
                type: "",
                isForget: "1"
            }
        }
    }

    LeftClick() {
    }

    showWarn(text) {
        this.setState({showWarn: true, tipText: text});

        this.state.warnTimer = setTimeout(()=> {
            this.setState({showWarn: false});
        }, 2000);
    }

    checkCode(phoneNum, code) {
        let fromData = new FormData();
        fromData.append("code", code)
        fromData.append("mobile", phoneNum)
        fetch(getHost() + "/user/resetPassword/identify/comparison", {
            method: 'POST',
            credentials: 'include',
            body: fromData
        }).then(response => response.json()).then(json => {
            if (json.success) {
                markDataRequestFlag();
                location.hash = "/NewSetPassword?phoneNum=" + phoneNum;
                return true;
            } else {
                this.showWarn("验证失败，请重新输入");
                return;
            }
        }).catch(e => {
            this.showWarn("网络出现了点问题：");
        });
    }

    forgetPassword(phoneNum, code) {
        this.checkCode(phoneNum, code)
    }

    render() {
        return (
            <div>
                <NavigationBar Title="忘记密码" LeftBar="true" LeftTitle="返回"
                               LeftIconSrc="/iot-meeting/assets/images/common/nav_back_icon.png" LeftClick={this.LeftClick}/>
                <RegisterItem data={this.state.data} callbackParent={this.forgetPassword}/>
                <Toptips type="warn" show={this.state.showWarn}>{this.state.tipText}</Toptips>
            </div>
        )
    }
}